package song;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Aleksey
 * Date: 02.01.2008
 * Time: 18:53:47
 */
public class TimeSignature {
    private Boolean doSimplify;
    private Integer beatNumber = 0;
    private Integer beatUnit = 0;

    public TimeSignature(Boolean doSimplify) {
        this.doSimplify = doSimplify;
    }

    public TimeSignature(Boolean doSimplify, Integer beatNumber, Integer beatUnit) {
        this(doSimplify);
        this.beatNumber = beatNumber;
        this.beatUnit = beatUnit;
        if (doSimplify) {
            simplify();
        }
    }

    public static TimeSignature sum(TimeSignature timeSignature1, TimeSignature timeSignature2) {
        TimeSignature timeSignature = new TimeSignature(true);
        Integer maxBeatUnit = timeSignature1.beatUnit > timeSignature2.beatUnit ? timeSignature1.beatUnit : timeSignature2.beatUnit;
        Integer beatNumber1 = timeSignature1.beatNumber;
        for (int i = 0 ; i < maxBeatUnit - timeSignature1.beatUnit; i++) {
            beatNumber1 *= 2;
        }
        Integer beatNumber2 = timeSignature2.beatNumber;
        for (int i = 0 ; i < maxBeatUnit - timeSignature2.beatUnit; i++) {
            beatNumber2 *= 2;
        }
        timeSignature.beatNumber = beatNumber1 + beatNumber2;
        timeSignature.beatUnit = maxBeatUnit;
        timeSignature.simplify();
        return timeSignature;
    }

    public ArrayList<TimeSignature> divide() {
        ArrayList<TimeSignature> division = new ArrayList<TimeSignature>();
        if (beatNumber == 1) {
            TimeSignature timeSignature1 = new TimeSignature(true);
            timeSignature1.setBeatNumber(1);
            timeSignature1.setBeatUnit(beatUnit + 1);
            division.add(timeSignature1);

            TimeSignature timeSignature2 = new TimeSignature(true);
            timeSignature2.setBeatNumber(1);
            timeSignature2.setBeatUnit(beatUnit + 1);
            division.add(timeSignature2);
        }
        else {
            TimeSignature temp = new TimeSignature(true);
            temp.setBeatNumber(beatNumber);
            temp.setBeatUnit(beatUnit);

            for (int i = 0; i < temp.beatNumber; i++) {
                TimeSignature t = new TimeSignature(true, 1, temp.beatUnit);
                division.add(t);
            }
        }
        return division;
    }

    public Integer getFullBeatUnit() {
        Integer fullUnit = 1;
        for (int i = 0; i < beatUnit; i++) {
            fullUnit *= 2;
        }
        return fullUnit;
    }

    public Integer getBeatNumber() {
        return beatNumber;
    }

    public void setBeatNumber(Integer beatNumber) {
        this.beatNumber = beatNumber;
        if (doSimplify) {
            simplify();
        }
    }

    public Integer getBeatUnit() {
        return beatUnit;
    }

    public void setBeatUnit(Integer beatUnit) {
        this.beatUnit = beatUnit;
        if (doSimplify) {
            simplify();
        }
    }

    private void simplify() {
        if (beatNumber == 0 || beatUnit == 0) {
            return;
        }
        while (beatNumber % 2 == 0) {
            beatNumber /= 2;
            beatUnit--;
        }
    }
}
